﻿using MediatR;
using Traceability.Commont.Domains.AnimalsDomainFiles;
using Traceability.Commont.Domains.BatchManagementFiles;
using Traceability.Commont.DTOs.CommontFiles;
using Traceability.Commont.DTOs.ReportDTOs;
using Traceability.Commont.Infrastructures.Interfaces;


namespace U.Enterprise.Api.Applications.Reports
{
    /// <summary>
    /// 白条汇总
    /// </summary>
    public class WhiteSummaryHander : IRequestHandler<WhiteSummaryCommand, Resqults<WhiteSummaryDTO>>
    {
        private readonly IBateRepository<WhiteNotes> white;
        private readonly IBateRepository<Shopping> shop;

        public WhiteSummaryHander(IBateRepository<WhiteNotes> white, IBateRepository<Shopping> shop)
        {
            this.white = white;
            this.shop = shop;
        }

        public async Task<Resqults<WhiteSummaryDTO>> Handle(WhiteSummaryCommand request, CancellationToken cancellationToken)
        {
            try
            {
                Resqults<WhiteSummaryDTO> res = new Resqults<WhiteSummaryDTO>();
                var list = (from a in white.GetAll()
                           join b in shop.GetAll() on a.ShoppingDesignationid equals b.ShoppingDesignationid
                           group new { a, b } by new { a.ShoppingDesignationid,b.ShoppingDesignationName,a.ScratchTheFat } into c
                           select new WhiteSummaryDTO()
                           {
                               ShoppingDesignationid=c.Key.ShoppingDesignationName,
                               WhiteNoteNum=c.Sum(x=>x.a.WhiteNoteNum),
                               NetWeight=c.Sum(x=>x.a.NetWeight),
                               ScratchTheFat= c.Key.ScratchTheFat
                           }).ToList();
                res.Date = list;
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
